package com.dpmc.jms;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.common.util.ObjectUtils;
import com.core.service.BaseService;

public class DataTransmitter {
	BaseService svc;
	
	public static void main(String[] args){
		DataTransmitter dt = new DataTransmitter();
		 
	    dt.fb_all("", null,null);
	}
	
	public List<String> getInsertSQLs(Map<String,List<Map>> params){
		List<String> sqls = new ArrayList<String>();
		for(String tabName :params.keySet()){
			List<Map> datas = params.get(tabName); 
			for(Map data:datas){
				StringBuffer sql = new StringBuffer();
				sql.append("insert into ").append(tabName).append("(");
				
				for(Object key:data.keySet()){
					sql.append(key).append(",");
				}
				sql.append(") values(");
				for(Object key:data.keySet()){
					sql.append("'").append(data.get(key)).append("'").append(",");
				}
				
				sql.append(")");
				sqls.add(sql.toString());
			}
		}
		return sqls;
	}
	

	public Map<String,List<Map>> fb_all(String id, Map param, Map JBXX) {

		Map retMap = new HashMap();

		HashMap queryParam = new HashMap();
		queryParam.put("SHID", id);

		SAXReader reader = new SAXReader();
		InputStream in = DataTransmitter.class.getClassLoader()
				.getResourceAsStream("com/dpmc/jms/XXFB.xml");

		try {
			Document doc = reader.read(in);
			List<Element> list_dataSet = doc.selectNodes("//dataSet");
	 
            Map<String,List<Map>> dataSetMap = new HashMap<String,List<Map>>();
        	for (Element e_dataSet : list_dataSet) {

    			String dataSetID = e_dataSet.attributeValue("id");
    			String svc = e_dataSet.attributeValue("svc");
    			String opname = e_dataSet.attributeValue("opname");
    			List<Element> params = e_dataSet.selectNodes("//param"); 
    			 
    			if(opname==null|| opname.trim().length()==0){
    				opname = "getResultList";
    			}
    			List p = new ArrayList(); 
    			for (Element node : params) {
    				String type = node.attributeValue("type");
    				String value = node.attributeValue("value");
    				
    				if("String".equals(type)){
    					p.add(value);
    				}else if("int".equals(type)){
    					p.add(ObjectUtils.getInt(value));
    				}else if("Map".equals(type)){
    					List<Element> values = node.selectNodes("//value");
    					Map mapValue = new HashMap();
    					p.add(mapValue);
    					for(Element v : values){
    						String v_type = v.attributeValue("type");
    						if(v_type==null||v_type.trim().length()==0){
    							v_type="String";
    						}
    						String v_name = v.attributeValue("name");
    						String v_value= v.getTextTrim();
    						if("String".equals(v_type)){
    							mapValue.put(v_name, v_value);
    						}else if("int".equals(v_type)){
    							mapValue.put(v_name, ObjectUtils.getInt(value)); 
    						} 
    					}
    				}
    				 
    			}
     System.out.println(p); 
    		 
    		 
    		}

	 
		//	System.out.println(dataSetMap);

		} catch (Exception e) {
			System.out.println("读取XML配置文件时，发现异常！");

		}
		return retMap;

	}

	private void fb_all_setcol(List<Map> d_table, String d_col, String s_query,
			String s_col, Map queryParam) {

		if (s_query != "") {

			// Map s_query_result = (Map) querySingleResult(s_query,
			// queryParam);
			if (d_table.isEmpty()) {
				// 如果表原本就是空的，则新增数据
				List<Map> s_query_result_list = (List<Map>) svc.getResultList(
						s_query, queryParam);
				for (Map s_query_result : s_query_result_list) {

					Map d_table_row = new HashMap();

					d_table_row.put(d_col, s_query_result.get(s_col));

					d_table.add(d_table_row);
				}
			} else {
				// 如果表中原来已有数据，则补上相应部分的字段信息
				List<Map> s_query_result_list = (List<Map>) svc.getResultList(
						s_query, queryParam);
				int count = 0;
				for (Map s_query_result : s_query_result_list) {
					// 此处循环赋值
					Map d_table_row = (Map) d_table.get(count);
					d_table_row.put(d_col, s_query_result.get(s_col));
					d_table.set(count, d_table_row);
					count++;

				}
			}

		}
	}

}
